package cn.xspace.wnx.util;

import android.content.Context;

import com.xspace.android.xafcommon.util.DateUtils;
import com.xspace.android.xafcommon.util.LogUtil;

import cn.xspace.chatui.bean.ChatFileEntity;
import cn.xspace.chatui.bean.MessageEntity;
import cn.xspace.chatui.bean.MsgRecordEntity;
import cn.xspace.library.util.MsgType;
import cn.xspace.library.util.MyInfoUtil;

import java.util.List;

import cn.xspace.wnx.R;
import cn.xspace.library.bean.OrgDeptUserBean;

/**
 * Created by liuchen on 2016/11/10.
 */

public class LocalGroupOperateHelper {
    private static MsgRecordEntity recordEntity = null;
    private static MessageEntity messageEntity = null;

    private static void commonDealChatRecord() {
        recordEntity = new MsgRecordEntity();
        recordEntity.msg_type = MsgType.CHAT_CROUP.toNumber();
        recordEntity.msg_content_type = MessageEntity.ContentType.SYSTEM_WARN.toNumber();
    }

    private static void commonDealMessageRecord() {
        messageEntity = new MessageEntity();
        messageEntity.msg_type = MsgType.CHAT_CROUP.toNumber();
        messageEntity.msg_content_type = MessageEntity.ContentType.SYSTEM_WARN.toNumber();
        messageEntity.msg_content = ChatFileEntity.getContentByType("", messageEntity.msg_content_type);
    }

    private static void saveDbCommon() {
//        ChatRecordDao.getInstance().getIChatRecordOperation().insertMsgRecord(recordEntity)
//                .subscribeOn(Schedulers.io())
//                .subscribe(new Action1<String>() {
//                    @Override
//                    public void call(String s) {
//                        LogUtil.d(s);
//                    }
//                });
//        MessageRecordDao.getInstance().getiMessageData().updateMessageList(messageEntity)
//                .subscribeOn(Schedulers.io())
//                .subscribe(new Action1<String>() {
//                    @Override
//                    public void call(String s) {
//                        LogUtil.d(s);
//                    }
//                });
    }

    //邀请成员加入群组
    public static void inviteIntoGroup(Context mContext, String groupId, List<OrgDeptUserBean> userList) {
        StringBuffer userSb = new StringBuffer();
        for (OrgDeptUserBean bean : userList) {
            if (!bean.user_id.equals(MyInfoUtil.getMyUserid(mContext))) {
                userSb.append(bean.user_name + "、");
            }
        }
        if (userSb.lastIndexOf("、") != -1) {
            userSb.deleteCharAt(userSb.lastIndexOf("、"));
        }
        //你邀请谁加入群组
        diffOperate(groupId,
                String.format(mContext.getString(R.string.you_invite_who_to_group), userSb.toString()), MessageEntity.SystemType.GroupNameChange);
        LogUtil.d(userSb.toString());
    }

    private static void diffOperate(String groupId, String showText, MessageEntity.SystemType type) {
        if (recordEntity == null) {
            commonDealChatRecord();
        }
        if (messageEntity == null) {
            commonDealMessageRecord();
        }
        messageEntity.msg_group_id = groupId;
        recordEntity.msg_group_id = groupId;
        recordEntity.tip_type = type.toNumber();
        messageEntity.tip_type = type.toNumber();
        recordEntity.msg_content = showText;
        recordEntity.msg_last_time = DateUtils.getNowDateTime();
        messageEntity.msg_last_time = DateUtils.getNowDateTime();
        recordEntity.inserttime = System.currentTimeMillis() + "";
        saveDbCommon();
    }

    /**
     * 群主更换
     *
     * @param mContext
     * @param groupId
     * @param groupOwnerNewName
     */
    public static void updateGroupOwner(Context mContext, String groupId, String groupOwnerNewName) {
        diffOperate(groupId, String.format(mContext.getString(R.string.who_become_new_group_owner), groupOwnerNewName), MessageEntity.SystemType.GroupRemoveMember);
    }

    /**
     * 更新群组名称
     *
     * @param mContext
     * @param groupId
     * @param groupNewName
     */
    public void updateGroupName(Context mContext, String groupId, String groupNewName) {
        diffOperate(groupId, String.format(mContext.getString(R.string.who_update_group_name), mContext.getString(R.string.you), groupNewName), MessageEntity.SystemType.GroupNameChange);
    }

    /**
     * 群组成员剔除
     *
     * @param mContext
     * @param groupId
     * @param userList
     */
    public static void removeMemberFromGroup(Context mContext, String groupId, List<OrgDeptUserBean> userList) {
        StringBuffer userSb = new StringBuffer();
        for (OrgDeptUserBean bean : userList) {
            if (!bean.user_id.equals(MyInfoUtil.getMyUserid(mContext))) {
                userSb.append(bean.user_name + "、");
            }
        }
        if (userSb.lastIndexOf("、") != -1) {
            userSb.deleteCharAt(userSb.lastIndexOf("、"));
        }
        diffOperate(groupId, String.format(mContext.getString(R.string.who_remove_who_from_group),
                mContext.getString(R.string.you), userSb.toString()), MessageEntity.SystemType.GroupRemoveMember);
    }

    public static void memberExit() {
        //无提示
    }


}
